package com.ysm.smfu.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.JiFenQrActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/14.
 */
public class JiFenFragment extends MyBaseFragment {

    @BindView(R.id.act_payset_name)
    TextView actPaysetName;
    @BindView(R.id.act_payset_num)
    EditText actPaysetNum;
    @BindView(R.id.act_payset_jifen)
    EditText actPaysetJifen;
    @BindView(R.id.act_payset_beizhu)
    EditText actPaysetBeizhu;
    private String shanghuName;
    private String businessId;
    private String name;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.jifenfragment);
        ButterKnife.bind(this, view);
//        输入小数点
//        actPaysetNum.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//        actPaysetNum.setKeyListener(new DigitsKeyListener(false, true));
//        小数点后输入两位
        UIUtils.setDeciNum(actPaysetNum, 2);

        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        shanghuName = currShared.getString(SpKey.SHANGHUNAME, "");
        businessId = currShared.getString(SpKey.BUSSINESSID, "");
        if (TextUtils.isEmpty(shanghuName)) {
//            联网获取商家用户名
            ToastUtils.showToast("无法获取商户名，请重新登录");
        }
        actPaysetName.setText(shanghuName);
        return view;
    }

    @OnClick({R.id.next})
    public void onClick(View view) {
        String money = actPaysetNum.getText().toString().trim();
        String jifen = actPaysetJifen.getText().toString().trim();
        String beizhu = actPaysetBeizhu.getText().toString().trim();
        if (TextUtils.isEmpty(beizhu))
            beizhu = "无";
        if (TextUtils.isEmpty(jifen)) {
            ToastUtils.showToast("消费兑换券为空");
            return;
        } else if (TextUtils.equals(jifen, "0")) {
            ToastUtils.showToast("兑换券不能为0");
            return;
        }
        getJiFenCode(money, jifen, beizhu);
    }

    /**
     * 获取兑换券支付订单号 /business/placePayOrderPoints
     * totalPrice:金额
     * totalPointsNum:兑换券
     * businessId:商户ID
     * userId:用户ID
     * remark:备注
     *
     * @param money
     * @param jifen
     * @param beizhu
     */
    private void getJiFenCode(String money, String jifen, String beizhu) {
        Intent intent = new Intent(getActivity(), JiFenQrActivity.class);
        intent.putExtra(SpKey.MONEY, money);
        intent.putExtra(SpKey.JIFEN, jifen);
        intent.putExtra(SpKey.REMARK, beizhu);
        intent.putExtra(SpKey.Pay_BUSSINESSID, businessId);
        startActivity(intent);
    }

    /**
     * 提示对话框--没有点击事件
     *
     * @param activity
     * @param msg
     */
    public void showErrorDialog(FragmentActivity activity, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(activity).setMessage(msg)
                .setNegativeButton("确定", null).create();
        dialog.show();
    }
}
